﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronBrokers.InformationBroker.ClientManagerService;
using System.Collections.ObjectModel;
using System.Windows;
using System.ComponentModel;
using System.Windows.Input;
using IronBrokers.InformationBroker.Helpers;

namespace IronBrokers.InformationBroker.Model
{
    public class UsersWindowViewModel : ViewModelBase
    {
        #region Public properties
        public ObservableCollection<User> Users { get; set; }

        public String NewUserName { get; set; }
        public String NewUserPassword { get; set; }

        private User selectedUser = null;
        public User SelectedUser
        {
            get
            {
                return selectedUser;
            }
            set
            {
                selectedUser = value;
                UpdateProperty("IsUserSelected");
            }
        }

        public Boolean IsUserSelected
        {
            get
            {
                return SelectedUser != null;
            }
        } 
        #endregion

        #region GetUserList

        public ICommand GetUserListCommand
        {
            get
            {
                return new RelayCommand(param => this.GetUserList());
            }
        }

        public void GetUserList()
        {
            try
            {
                using (ClientManagerServiceClient svc = new ClientManagerServiceClient())
                {
                    UserListResponse response = svc.GetUserList();
                    if (response.Success)
                    {
                        Users = new ObservableCollection<User>(response.UserList);
                        UpdateProperty("Users");
                    }
                    else
                    {
                        throw new Exception(response.ErrorMessage);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred during getting user list.\n" + ex.Message);
            }
        } 
        #endregion

        #region EditPriviledges
        public ICommand EditPriviledgesCommand
        {
            get
            {
                return new RelayCommand(param => EditPriviledges());
            }
        }

        public void EditPriviledges()
        {
            PriviledgesWindow window = new PriviledgesWindow(SelectedUser);
            window.ShowDialog();
        } 
        #endregion

        #region RegisterUser
        
        public ICommand RegisterUserCommand
        {
            get
            {
                return new RelayCommand(param => this.RegisterUser());
            }
        }

        public void RegisterUser()
        {
            try
            {
                using (ClientManagerServiceClient svc = new ClientManagerServiceClient())
                {
                    Response response = svc.RegisterUser(NewUserName, PasswordHelper.SH1(NewUserPassword));
                    if (response.Success)
                    {
                        Users.Add(new User()
                        {
                            Name = NewUserName
                        });
                        UpdateProperty("Users");
                        NewUserName = null;
                        UpdateProperty("NewUserName");
                        NewUserPassword = null;
                        UpdateProperty("NewUserPassword");
                        MessageBox.Show("New user registered successfully.");
                    }
                    else
                    {
                        throw new Exception(response.ErrorMessage);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred during registering a new user.\n" + ex.Message);
            }
        } 
        #endregion

        #region UnregisterUser
        public ICommand UnregisterUserCommand
        {
            get
            {
                return new RelayCommand(param => this.UnregisterUser());
            }
        }

        public void UnregisterUser()
        {
            if (SelectedUser != null)
            {
                try
                {
                    using (ClientManagerServiceClient svc = new ClientManagerServiceClient())
                    {
                        Response response = svc.UnregisterUser(SelectedUser.Name);
                        if (response.Success)
                        {
                            User userToDelete = Users.Where(x => x.Name.Equals(SelectedUser.Name)).FirstOrDefault();
                            Users.Remove(userToDelete);
                            SelectedUser = null;
                            UpdateProperty("SelectedUser");
                            UpdateProperty("Users");
                            MessageBox.Show("User unregistered successfully.");
                        }
                        else
                        {
                            throw new Exception(response.ErrorMessage);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("An error occurred during unregistering user.\n" + ex.Message);
                }
            }
        } 
        #endregion

    }
}
